package com.baske.producer;

import com.baske.conf.JmsConfig;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.stereotype.Component;

/**
 * 生产者
 */
@Component
public class PayProducer {

    // 生产分组
    private String producerGroup = "pay_producer_group";

    private String nameServerAddr = "192.168.0.104:9876";

    private DefaultMQProducer producer;

    public PayProducer () {
        producer = new DefaultMQProducer(producerGroup);
        // Name server
        producer.setNamesrvAddr(JmsConfig.NAME_SERVER);
        // 失败重试次数
        producer.setRetryTimesWhenSendAsyncFailed(3);
        start();
    }

    public DefaultMQProducer getProducer() {
        return this.producer;
    }

    public void start() {
        try {
            this.producer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
    }

    public void shutdown() {
        this.producer.shutdown();
    }
}
